package website.tools;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

import website.Button;
import website.Content;
import website.DataBase;
import website.Page;
import website.Form;
import website.Website;
import website.WebsitePackage;

public class WebsiteSerializer {

	/**
	 * Load the web site model.
	 * @param f the file to load
	 * @return the object representation of the web site described by the file
	 */
	public Website load(File f) { 
		ResourceSet rs = new ResourceSetImpl(); 
		Resource.Factory.Registry registry =  rs.getResourceFactoryRegistry(); 
		Map<String,Object> m = registry.getExtensionToFactoryMap();   
		m.put("xmi", new XMIResourceFactoryImpl()); 
		rs.getPackageRegistry().put(WebsitePackage.eNS_URI, 
				WebsitePackage.eINSTANCE); 
		URI uri = URI.createFileURI(f.getAbsolutePath()); 
		Resource resource = rs.getResource(uri, true);   

		if(resource.isLoaded() && resource.getContents().size() > 0) {    
			return(Website) resource.getContents().get(0); 
		}    
		return null; 
	} 

	public void save(Website stage, File f) { 
		ResourceSet rs = new ResourceSetImpl(); 
		Resource.Factory.Registry registry =  rs.getResourceFactoryRegistry(); 

		Map<String,Object> m = registry.getExtensionToFactoryMap();   
		m.put("xmi", new XMIResourceFactoryImpl()); 
		m.put("ecore", new EcoreResourceFactoryImpl()); 
		rs.getPackageRegistry().put(WebsitePackage.eNS_URI, 
				WebsitePackage.eINSTANCE); 
		Resource packageResource =  rs.createResource(URI.createFileURI("Turtle.ecore")); 

		packageResource.getContents().add(WebsitePackage.eINSTANCE);   try{ 
			packageResource.load(null); 

		} catch(IOException e1) { 
			e1.printStackTrace();   
		}   
		URI uri = URI.createFileURI(f.getAbsolutePath()); 
		Resource resource = rs.createResource(uri);   
		resource.getContents().add(stage);   
		try{ 
			HashMap<String, Boolean> options = new HashMap<String,Boolean>(); 
			options.put(XMIResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE); 
			resource.save(options);   
		} catch(IOException e) { e.printStackTrace(); } 
	} 


	public DataBase load_DataBase(File f) { 
		ResourceSet rs = new ResourceSetImpl(); 
		Resource.Factory.Registry registry =  rs.getResourceFactoryRegistry(); 
		Map<String,Object> m = registry.getExtensionToFactoryMap();   
		m.put("xmi", new XMIResourceFactoryImpl()); 
		rs.getPackageRegistry().put(WebsitePackage.eNS_URI, 
				WebsitePackage.eINSTANCE); 
		URI uri = URI.createFileURI(f.getAbsolutePath()); 
		Resource resource = rs.getResource(uri, true);   

		if(resource.isLoaded() && resource.getContents().size() > 0) {    
			return(DataBase) resource.getContents().get(0); 
		}    
		return null; 
	} 

	public void save(DataBase DataBase, File f) { 
		ResourceSet rs = new ResourceSetImpl(); 
		Resource.Factory.Registry registry =  rs.getResourceFactoryRegistry(); 

		Map<String,Object> m = registry.getExtensionToFactoryMap();   
		m.put("xmi", new XMIResourceFactoryImpl()); 
		m.put("ecore", new EcoreResourceFactoryImpl()); 
		rs.getPackageRegistry().put(WebsitePackage.eNS_URI, 
				WebsitePackage.eINSTANCE); 
		Resource packageResource =  rs.createResource(URI.createFileURI("Turtle.ecore")); 

		packageResource.getContents().add(WebsitePackage.eINSTANCE);   try{ 
			packageResource.load(null); 

		} catch(IOException e1) { 
			e1.printStackTrace();   
		}   
		URI uri = URI.createFileURI(f.getAbsolutePath()); 
		Resource resource = rs.createResource(uri);   
		resource.getContents().add(DataBase);   
		try{ 
			HashMap<String, Boolean> options = new HashMap<String,Boolean>(); 
			options.put(XMIResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE); 
			resource.save(options);   
		} catch(IOException e) { e.printStackTrace(); } 
	} 


	public void updateWebsite (Website website, EList<DataBase> databaseList){
		EList<Page> pages =  website.getPageList();

		for(Page p : pages){
			EList<Content> contents = p.getContents();
			if(contents != null){ 
				for(Content c : contents){
					if((c instanceof Button) | (c instanceof Form)){
						((Button) c).getTriggeredAction().getDatasource().clear();
						((Button) c).getTriggeredAction().getDatasource().addAll(databaseList);
					}
				}
			}
		}
	}


}
